package com.dao;

import com.model.MailReceiver;
import com.model.MailSender;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate4.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

/**
 * @author zooy
 * @since 2024/6/20-14:30
 */
@Repository("mailReceiveDao")
public class MailReceiveDao extends HibernateDaoSupport {

    /**
     * 保存数据到数据库
     *
     * @param mailReceiver
     */
    public void save(MailReceiver mailReceiver) {
        getHibernateTemplate().save(mailReceiver);
    }

    /**
     * 更新数据到数据库
     *
     * @param mailReceiver
     */
    public void update(MailReceiver mailReceiver) {
        getHibernateTemplate().update(mailReceiver);
    }

    /**
     * 删除数据
     *
     * @param mailReceiver
     */
    public void delete(MailReceiver mailReceiver) {
        getHibernateTemplate().delete(mailReceiver);
    }

    /**
     * 根据ID查询数据
     *
     * @param id
     * @return
     */
    public MailSender findById(Integer id) {
        return getHibernateTemplate().get(MailSender.class, id);
    }

    /**
     * 查询数据
     *
     * @param mailReceiver
     * @param offset
     * @param pageSize
     * @return
     */
    public List<MailSender> findAll(MailReceiver mailReceiver, Integer offset, Integer pageSize) {

        List<MailSender> list = new ArrayList<>();
        Session session = null;
        try {
            session = getHibernateTemplate().getSessionFactory().openSession();
            String hsql = " from MailReceiver where 1=1 ";
            hsql = getSQL(mailReceiver, hsql);
            hsql = hsql + " order by id desc";
            Query query = session.createQuery(hsql);
            if (offset != null && pageSize != null) {
                query.setFirstResult(offset);
                query.setMaxResults(pageSize);
            }
            list = query.list();
        } catch (Exception re) {
            re.printStackTrace();
        } finally {
            if (session != null) {
                session.close();
            }
        }
        return list;
    }

    /**
     * 查询记录总条数
     *
     * @param mailSender
     * @return
     */
    public int getCount(MailReceiver mailReceiver) {
        int count = 0;
        Session session = null;
        try {
            session = getHibernateTemplate().getSessionFactory().openSession();
            String hsql = " select count(*) from MailReceiver where 1=1 ";
            hsql = getSQL(mailReceiver, hsql);
            Query query = session.createQuery(hsql);
            count = ((Long) query.uniqueResult()).intValue();
        } catch (Exception re) {
            re.printStackTrace();
        } finally {
            if (session != null) {
                session.close();
            }
        }
        return count;
    }

    public String getSQL(MailReceiver mailReceiver, String sql) {
        if (StringUtils.isNotBlank(mailReceiver.getSubject())) {
            sql = sql + " and subject = '" + mailReceiver.getSubject() + "'";
        }
        return sql;
    }

}
